package com.hoshiicloud.mallshop.controller.goodsmanage;

import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.mallshop.param.ProductSpuParam;
import com.hoshiicloud.mallshop.service.goodsmanage.MainStoreGoodsManageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 商品管理： 商品列表展示，商品的删除， 上下架， 编辑
 * @author linjx on 2019/9/17
 */
@Api(value = "商品管理-商品管理", tags = {"商品管理-商品管理"})
@RestController
@RequestMapping("/goods-manage/main-store-goods-manage")
public class MainStoreGoodsManageController {
    private final MainStoreGoodsManageService productManageService;
    public MainStoreGoodsManageController(MainStoreGoodsManageService productManageService) {
        this.productManageService = productManageService;
    }

    @ApiOperation(value = "分页查询商品列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNumber", value = "当前页", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "总页数", dataType = "Integer"),
            @ApiImplicitParam(name = "orderField", value = "需要排序的字段", dataType = "String"),
            @ApiImplicitParam(name = "orderType", value = "排序方式", dataType = "String"),
            @ApiImplicitParam(name = "status", value = "上下架状态", dataType = "1-上架 2-下架"),
            @ApiImplicitParam(name = "title", value = "商品名称", dataType = "String"),
            @ApiImplicitParam(name = "categoryId", value = "类目Id", dataType = "String"),
            @ApiImplicitParam(name = "skuNo", value = "商品编号", dataType = "Long")
    })
    @PostMapping("/listProduct")
    public Response listProduct(@RequestBody Map<String, Object> map) {

       // TokenUsersVO user = UserInfoUtil.getUserInfo(rq);
        map.put("companyId", 3171776001692663893L);
        map.put("platformId",3189609977215975481L);
        map.put("storeId", 58L);

        return Response.data(productManageService.listProduct(map));
    }

    @ApiOperation(value = "商品上架")
    @ApiImplicitParam(name = "extId", value = "extSpuId", dataType = "Integer")
    @PostMapping("/productShelvesUp")
    public Response productShelvesUp(@RequestBody Map<String, Object> map) {
        //TokenUsersVO user = UserInfoUtil.getUserInfo(rq);
        map.put("companyId", 3171776001692663893L);
        map.put("platformId", 3189609977215975481L);
        map.put("storeId",58L);
        map.put("updateBy", "小明");
        return Response.data(productManageService.productShelvesUpAndDown(map, 1));
    }

    @ApiOperation(value = "商品下架")
    @ApiImplicitParam(name = "extId", value = "extSpuId", dataType = "Integer")
    @PostMapping("/productShelvesDown")
    public Response productShelvesDown(@RequestBody Map<String, Object> map) {
        //TokenUsersVO user = UserInfoUtil.getUserInfo(rq);
        map.put("companyId", 3171776001692663893L);
        map.put("platformId", 3189609977215975481L);
        map.put("storeId",58L);
        map.put("updateBy", "小明");
        return Response.data(productManageService.productShelvesUpAndDown(map, 2));
    }

    @ApiOperation(value = "批量删除商品")
    @ApiImplicitParam(name = "extSpuIds", value = "extSpuId集合", required = true, dataType = "Array")
    @DeleteMapping("/deleteProduct")
    public Response deleteProductBatch(@RequestBody Map<String, Object> map) {
        map.put("companyId", 3171776001692663893L);
        map.put("platformId", 3189609977215975481L);
        map.put("storeId",58L);
        map.put("updateBy", "小明");
        return Response.data(productManageService.deleteProductBatch(map));
    }

    @ApiOperation(value = "获取需要编辑的商品信息")
    @PostMapping("/getNeedEditProduct")
    public Response getNeedEditProduct(@RequestBody Map<String, Object> map) {
        map.put("companyId", 3171776001692663893L);
        map.put("platformId", 3189609977215975481L);
        map.put("storeId",58L);
        map.put("updateBy", "小明");
        return Response.data(productManageService.getNeedEditProduct(map));
    }

    @ApiOperation(value = "商品编辑")
    @PostMapping("/editProduct")
    public Response editProduct(@RequestBody ProductSpuParam spuParam) {

        spuParam.setCompanyId(3171776001692663893L);
        spuParam.setPlatformId(3189609977215975481L);
        spuParam.setStoreId(58L);
        spuParam.setUpdateBy("小红");
        if (spuParam.getEditType() == 1) {
            // 编辑总店商品
            return Response.data(productManageService.editProductForHeadStore(spuParam));
        }else if (spuParam.getEditType() == 2) {
            // 编辑总店商品并且同步到分店
            return Response.data(productManageService.editAllProduct(spuParam));
        }else {
            return Response.failed("编辑失败，无此选项");
        }

    }
}
